package com.example.demo.questions.service.impl;

import com.example.demo.answers.entity.TAnswers;
import com.example.demo.answers.mapper.TAnswersMapper;
import com.example.demo.enums.ErrorEnum;
import com.example.demo.exception.ZException;
import com.example.demo.questions.entity.TQuestions;
import com.example.demo.questions.mapper.TQuestionsMapper;
import com.example.demo.questions.service.ITQuestionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 考题表 服务实现类
 * </p>
 *
 * @author zhongz
 * @since 2019-02-16
 */
@Service
public class TQuestionsServiceImpl extends ServiceImpl<TQuestionsMapper, TQuestions> implements ITQuestionsService {

    @Resource
    private TQuestionsMapper questionsMapper;

    @Resource
    private TAnswersMapper answersMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateQues(Map<String, Object> paramMap) throws Exception {
        // 拼装考题并更新
        TQuestions question = new TQuestions();
        if (paramMap.get("id") != null) {
            question.setId(paramMap.get("id").toString());
        } else {
            throw new ZException(ErrorEnum.LACK_PARAM.getValue());
        }
        if (paramMap.get("quesContent") != null) {
            question.setQuesContent(paramMap.get("quesContent").toString());
        } else {
            throw new ZException(ErrorEnum.LACK_PARAM.getValue());
        }
        if (paramMap.get("rightAnswerId") != null) {
            question.setRightAnswerId(paramMap.get("rightAnswerId").toString());
        } else {
            throw new ZException(ErrorEnum.LACK_PARAM.getValue());
        }
        if (paramMap.get("analysis") != null) {
            question.setAnalysis(paramMap.get("analysis").toString());
        }
        if (paramMap.get("answerTotalCount") != null) {
            question.setAnswerTotalCount((int) paramMap.get("answerTotalCount"));
        } else {
            throw new ZException(ErrorEnum.LACK_PARAM.getValue());
        }
        questionsMapper.updateById(question);

        // ------ 拼装答案并更新 ------
        // 正确答案
        TAnswers rightAnswer = new TAnswers();
        rightAnswer.setId(question.getRightAnswerId());
        rightAnswer.setQuestionId(question.getId());
        rightAnswer.setAnsContent(StringUtils.trim(paramMap.get("right").toString()));
        answersMapper.updateById(rightAnswer);
        // 错误答案
        String[] wrongAnswers = StringUtils.split(StringUtils.trim(paramMap.get("wrong").toString()), "|");
        String[] wrongAnswerIds = StringUtils.split(paramMap.get("wrongIds").toString(), "|");
        for (int i = 0; i < wrongAnswers.length; i++) {
            TAnswers wrongAnswer = new TAnswers();
            wrongAnswer.setId(wrongAnswerIds[i]);
            wrongAnswer.setQuestionId(question.getId());
            wrongAnswer.setAnsContent(StringUtils.trim(wrongAnswers[i]));
            answersMapper.updateById(wrongAnswer);
        }
        return true;
    }
}
